﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Reflection;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using ECare.BUS;
using ECare.BUS.Data;
using ECare.Client.frms;

namespace ECare.Client
{
    public partial class frmMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public frmMain()
        {
            InitializeComponent();

            this.Load += frmMain_Load;
        }

        void frmMain_Load(object sender, EventArgs e)
        {
            //throw new NotImplementedException();
            this.LoadMenus();

            //var frmHome = new frmHome();
            //frmHome.MdiParent = this;
            //frmHome.Show();
        
        }


        private void LoadMenus()
        {
            ribbon.Pages.Clear();
            var items = ModulesHelper.GetMenuItems();

       //     var rolepermission = RoleInfoHelper.GetRoles().FirstOrDefault(a=>a.RoleId==ApplicationData.CurrentUserInfo.RoleId);

         //   ApplicationData.Permissions = rolepermission.Permissions;

            var topitems = items.Where(a => a.ModuleParentId == -1);
            foreach (var item in topitems)
            {

    //            if(!rolepermission.Permissions.Modules.Contains(item.ModuleId))
           //         continue;
                
                var page = new RibbonPage();
                page.Text = item.ModuleName;
                page.Tag = item.frmType;
                var group = new RibbonPageGroup("相关功能");
                page.Groups.Add(group);
                this.ribbon.Pages.Add(page);
                this.LoadChildren(item, group);
            }
        }

        private void LoadChildren(ModuleInfo currentModule, RibbonPageGroup currentItem)
        {if (currentModule.Children.Count == 0)
                return;
            foreach (var item in currentModule.Children)
            {

            //    if (!ApplicationData.Permissions.Modules.Contains(item.ModuleId))
             //       continue;

                var newItem = new BarButtonItem { Caption = item.ModuleName, Tag = item.frmType, LargeImageIndex = item.ImageIndex };
                newItem.ItemClick += newItem_ItemClick;
                newItem.RibbonStyle = DevExpress.XtraBars.Ribbon.RibbonItemStyles.Large; currentItem.ItemLinks.Add(newItem);
            }
            }

        void newItem_ItemClick(object sender, ItemClickEventArgs e)
        {


            var item = e.Item as BarButtonItem;
            string currentfrm = item.Tag.ToString();
            if (string.IsNullOrEmpty(currentfrm))
                return;

            //  if(this.OwnedForms.Where(a=>a.GetType().Equals(item.Tag.ToString() ) ))

            var frm = this.MdiChildren.FirstOrDefault(a => a.GetType().FullName.Equals(currentfrm));
            if (frm != null)
            {
                frm.Activate(); return;
            }

            Assembly asm = Assembly.GetExecutingAssembly();
            frm = asm.CreateInstance(item.Tag.ToString()) as Form;
            frm.MdiParent = this;
            frm.WindowState = FormWindowState.Maximized;
            frm.Show();
        }



        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            var frm = new frmCheck();
            frm.MdiParent = this;
            frm.Show();
        }
    }
}